#include "stdafx.h"
#include <MyControls/StdProperties.h>
#include <MyControls/PropertyString.h>
#include <MyControls/Resource.h>

namespace My {

template <>
bool TypedControlBase<String>::GetText(String & s)
{
	return m_Property.GetValue(m_Objects, s);
}

template <>
void TypedControlBase<String>::SetText(const String & s)
{
	m_Property.SetValue(m_Objects, s);
}

template <> 
bool TypedControlBase<Size>::GetText(String & s)
{
	My::Size value;
	if (!m_Property.GetValue(m_Objects, value))
		return false;
	std::ostringstream out;
	out << (unsigned int) value;
	s = out.str();
	return true;
}

template <>
void TypedControlBase<Size>::SetText(const String & s)
{
	long int value;
	std::istringstream in;
	in.str(s);
	in >> value;
	if (in.fail() || !in.eof() || value < 0)
		return;
	m_Property.SetValue(m_Objects, My::Size(value));
}


TypedControlBase<bool>::TypedControlBase(BasicProperty<bool> & p, const EditableList & list) :
	m_Property(p), m_Objects(list)
{
}

CheckBox::State TypedControlBase<bool>::GetState() const
{
	bool value;
	if (!m_Property.GetValue(m_Objects, value))
		return Indeterminate;
	else
		return value ? Checked : Unchecked;
}

void TypedControlBase<bool>::HandleClick()
{
	bool value = false;
	m_Property.GetValue(m_Objects, value);
	m_Property.SetValue(m_Objects, !value);
}

void TypedControlBase<bool>::HandleUpdateUI()
{
	SetVisible(m_Property.Visible(m_Objects));
	SetEnabled(!m_Property.ReadOnly(m_Objects));
}


TypedControlBase<ByteColor>::TypedControlBase(BasicProperty<ByteColor> & p, const EditableList & list) :
	m_Property(p), m_Objects(list)
{
}

bool TypedControlBase<ByteColor>::GetColor(COLORREF & c) const
{
	ByteColor value;
	if (!m_Property.GetValue(m_Objects, value))
		return false;
	c = RGB(value.r, value.g, value.b);
	return true;
}

void TypedControlBase<ByteColor>::SetColor(COLORREF c)
{
	ByteColor value;
	m_Property.GetValue(m_Objects, value); //preserve alpha component of original color
	value.b = GetBValue(c);
	value.g = GetGValue(c);
	value.r = GetRValue(c);
	m_Property.SetValue(m_Objects, value);
}

void TypedControlBase<ByteColor>::HandleUpdateUI()
{
	SetVisible(m_Property.Visible(m_Objects));
	SetEnabled(!m_Property.ReadOnly(m_Objects));
}


// Property<span> 

Property<Span>::Property()
{
}

void Property<Span>::Init()
{
	(* this)[MY_SPAN_FIRST] = &First.ReadWrite<Span>(* this, &span::first);
	(* this)[MY_SPAN_LAST] = &Last.ReadWrite<Span>(* this, &span::last);
}

// Property<span> 

Property<Extent>::Property()
{
}

void Property<Extent>::Init()
{
	(* this)[MY_EXTENT_WIDTH] = &Width.ReadWrite<Extent>(* this,  &Extent::sx);
	(* this)[MY_EXTENT_HEIGHT] = &Height.ReadWrite<Extent>(* this,  &Extent::sy);
}

// Property< point<float> >

Property< point<float> >::Property()
{
}

void Property< point<float> >::Init()
{
	(* this)["X"] = &X.ReadWrite(ReadWriteAs< Decimal<float,3> >(* this, &point<float>::x));
	(* this)["Y"] = &Y.ReadWrite(ReadWriteAs< Decimal<float,3> >(* this, &point<float>::y));
/*
	(* this)["X"] = &X.ReadWrite(* this, &point<float>::x);
	(* this)["Y"] = &Y.ReadWrite(* this, &point<float>::y);
*/
}

Widget * Property< point<float> >::CreateControl(const EditableList & list)
{
	DoInit();
	return new RowPropertyControl(* this, list);
}

void Property< point<float> >::FreeControl(Widget * pWidget)
{
	delete pWidget;
}


} // My